﻿<%@ WebHandler Language="C#" Class="ZSExchangeBillReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 装配试车厂周转串件报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2023.5.6
 **/

public class ZSExchangeBillReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"
SELECT ex.billtype,ex.exchangename,pb.productname,p.description,ex.containerno,wfs.workflowstepname,u.attributevalue,
       ex.createdate,e.fullname createPerson,ex.comcontainername,ex.billsource,ex.reason,
       exd.operateemployeename,exd.operatedate qianshenDate,
       CASE WHEN exd.billstatus = '0' THEN '厂内未确认'
            WHEN exd.billstatus = '2' THEN '厂外未确认'
            WHEN exd.billstatus = '3' THEN '厂外确认中'
            WHEN exd.billstatus = '5' THEN '已退回' 
       END status,
       ex.machine,ex.operatedate, ex.workhours,ex.maprequest,'' actual,ex.note
FROM Exchangebill ex
LEFT JOIN Exchangebilldealinfo exd ON exd.exchangebillid = ex.exchangeid AND exd.isuse = '0'
LEFT JOIN product p ON ex.productid=p.productid
LEFT JOIN productbase pb ON p.productbaseid=pb.productbaseid
LEFT JOIN enginepartplan ep ON ex.containerid=NVL(ep.containerid,ep.enginecontainerid)
LEFT JOIN mfgorder m ON m.mfgorderid=ep.mfgorderid
LEFT JOIN Userattribute u ON m.mfgorderid=u.parentid AND u.userattributename='发动机'
LEFT JOIN workflowstep wfs ON ep.workflowstepid=wfs.workflowstepid
LEFT JOIN employee e ON e.employeeid = ex.employeeid
WHERE ex.isuse=0
";
        //创建时间
        if (jObject["createdate"] != null && jObject["createdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createdate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND TRUNC(ex.createdate) >= to_date('{0}','yyyy-MM-dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(ex.createdate) <= to_date('{0}','yyyy-MM-dd')", arr[1].ToString());
            }
        }

        //创建人
        if (jObject["creater"] != null && jObject["creater"].ToString() != "")
        {
            sql += string.Format(" AND e.fullname LIKE '%{0}%'", jObject["creater"].ToString());
        }

        //发动机号
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND u.attributevalue = '{0}'", jObject["engineno"].ToString());
        }

        //类型
        if (jObject["billtype"] != null)
        {
            string billtype = jObject["billtype"].ToString();
            if (billtype == "周转件")
            {
                sql += string.Format(" AND ex.billtype='周转单'");
            }
            else
            {
                sql += string.Format(" AND ex.billtype='串件单'");
            }
        }

        //串上机号
        if (jObject["machine"] != null && jObject["machine"].ToString() != "")
        {
            sql += string.Format(" AND ex.machine LIKE '%{0}%'", jObject["machine"].ToString());
        }

        //零件来源
        if (jObject["billsource"] != null && jObject["billsource"].ToString() != "")
        {
            sql += string.Format(" AND ex.billsource LIKE '%{0}%'", jObject["billsource"].ToString());
        }

        //件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND pb.productname = '{0}'", jObject["productname"].ToString());
        }

        //任务单号
        if (jObject["containerno"] != null && jObject["containerno"].ToString() != "")
        {
            sql += string.Format(" AND ex.containerno = '{0}'", jObject["containerno"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        if (jObject["billtype"].ToString() == "周转件")
        {
            dt.Columns.Remove("machine");
            dt.Columns.Remove("operatedate");
            dt.Columns.Remove("workhours");
            dt.Columns.Remove("maprequest");
            dt.Columns.Remove("actual");
            dt.Columns.Remove("note");

            dt.Columns["billtype"].Caption = "类型";
            dt.Columns["exchangename"].Caption = "编号";
            dt.Columns["productname"].Caption = "件号";
            dt.Columns["description"].Caption = "件号名称";
            dt.Columns["containerno"].Caption = "任务单号";
            dt.Columns["workflowstepname"].Caption = "大阶段";
            dt.Columns["attributevalue"].Caption = "发动机号";
            dt.Columns["createPerson"].Caption = "创建人";
            dt.Columns["createdate"].Caption = "创建日期";
            dt.Columns["comcontainername"].Caption = "组合件号";
            dt.Columns["billsource"].Caption = "来源";
            dt.Columns["reason"].Caption = "原因";
            dt.Columns["operateemployeename"].Caption = "审签人";
            dt.Columns["qianshenDate"].Caption = "审签日期";
            dt.Columns["status"].Caption = "审签状态";
        }
        else
        {
            dt.Columns["billtype"].Caption = "类型";
            dt.Columns["exchangename"].Caption = "编号";
            dt.Columns["productname"].Caption = "件号";
            dt.Columns["description"].Caption = "件号名称";
            dt.Columns["containerno"].Caption = "任务单号";
            dt.Columns["workflowstepname"].Caption = "大阶段";
            dt.Columns["attributevalue"].Caption = "发动机号";
            dt.Columns["createPerson"].Caption = "创建人";
            dt.Columns["createdate"].Caption = "创建日期";
            dt.Columns["comcontainername"].Caption = "组合件号";
            dt.Columns["billsource"].Caption = "来源";
            dt.Columns["reason"].Caption = "原因";
            dt.Columns["operateemployeename"].Caption = "审签人";
            dt.Columns["qianshenDate"].Caption = "审签日期";
            dt.Columns["status"].Caption = "审签状态";
            dt.Columns["machine"].Caption = "串件机号";
            dt.Columns["operatedate"].Caption = "串件日期";
            dt.Columns["workhours"].Caption = "工作时间";
            dt.Columns["maprequest"].Caption = "图纸要求";
            dt.Columns["actual"].Caption = "实际";
            dt.Columns["note"].Caption = "备注";
        }
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + jObject["billtype"].ToString() + "报表");
    }
}